package com.hx.springboot1.security;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author: hx
 * Date: 2021-05-12 23/28
 * Description: Nothing else
 */
@Slf4j
@Component
public class MyLoginSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        User userInfo = (User) authentication.getPrincipal();
        log.info("用户登入成功：{}",userInfo.toString());
        /* 这里不是跳到/indexRequest， 是先跳到index.html 加载完静态资源后，触发jquery的 ready事件，异步的去controller的/indexRequest请求获取用户信息 */
        httpServletResponse.sendRedirect("/index");
    }
}
